package top.mcwebsite.playandroid.common.network.impl

import okhttp3.Interceptor
import okhttp3.Response
import top.mcwebsite.playandroid.common.network.ApiException
import top.mcwebsite.playandroid.common.network.NetworkApiException
import top.mcwebsite.playandroid.common.network.api.NetworkService
import top.mcwebsite.playandroid.common.network.noInternet
import top.mcwebsite.playandroid.lib.servicemanager.core.ServiceManager

class ConnectiveInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        if (!ServiceManager.getService<NetworkService>().isNetworkAvailable()) {
            throw NetworkApiException.noInternet()
        } else {
            return chain.proceed(chain.request())
        }
    }
}